package bll

import (
	"context"
	"maya-service/store/mysql"

	"maya-service/model"
	"maya-service/model/entity"
	"maya-service/model/mapping"
	"maya-service/store"
	"time"
)

type supervisorWithDraw struct {
	iSupervisorWithDraw store.ISupervisorWithDraw
}

var SupervisorWithDraw = &supervisorWithDraw{
	iSupervisorWithDraw: mysql.SupervisorWithDraw,
}

func init() {
	Register(SupervisorWithDraw)
}

func (a *supervisorWithDraw) init() func() {
	return func() {}
}

// Create
func (a *supervisorWithDraw) Create(ctx context.Context, in *model.SupervisorWithDrawCreateRequest) error {
	var (
		err error
	)
	c := buildSupervisorWithDraw(in)
	_, err = a.iSupervisorWithDraw.Create(ctx, c)
	return err
}

// Update
func (a *supervisorWithDraw) Update(ctx context.Context, in *model.SupervisorWithDrawUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	if in.SupervisorId != nil {
		dict["supervisor_id"] = in.SupervisorId
	}

	if in.SupervisorName != nil {
		dict["supervisor_name"] = in.SupervisorName
	}

	if in.ItemList != nil {
		dict["item_list"] = in.ItemList
	}

	if in.OrderCount != nil {
		dict["order_count"] = in.OrderCount
	}

	if in.TotalAmount != nil {
		dict["total_amount"] = in.TotalAmount
	}

	if in.DeductAmount != nil {
		dict["deduct_amount"] = in.DeductAmount
	}

	if in.Status != nil {
		dict["status"] = in.Status
	}

	if in.Desc != nil {
		dict["desc"] = in.Desc
	}

	if in.Operator != nil {
		dict["operator"] = in.Operator
	}

	if in.OperatorId != nil {
		dict["operator_id"] = in.OperatorId
	}

	if in.CreatedAt != nil {
		dict["created_at"] = in.CreatedAt
	}

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	// do other update here
	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt
	return a.iSupervisorWithDraw.Update(ctx, in.Id, dict)
}

// Delete
func (a *supervisorWithDraw) Delete(ctx context.Context, in *model.SupervisorWithDrawDeleteRequest) error {
	return a.iSupervisorWithDraw.Delete(ctx, in.Id)
}

// List
func (a *supervisorWithDraw) List(ctx context.Context, in *model.SupervisorWithDrawListRequest) (*model.SupervisorWithDrawListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.SupervisorWithDraw
		out   = &model.SupervisorWithDrawListResponse{}
	)

	if total, list, err = a.iSupervisorWithDraw.List(ctx, in); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = mapping.SupervisorWithDrawsEntityToDto(list)

	return out, nil
}

// Find
func (a *supervisorWithDraw) Find(ctx context.Context, in *model.SupervisorWithDrawInfoRequest) (*model.SupervisorWithDrawInfo, error) {
	var (
		err  error
		data *entity.SupervisorWithDraw
		out  = &model.SupervisorWithDrawInfo{}
	)

	if data, err = a.iSupervisorWithDraw.Find(ctx, in); err != nil {
		return nil, err
	}

	out = mapping.SupervisorWithDrawEntityToDto(data)
	return out, nil
}

// buildSupervisorWithDraw build entity
func buildSupervisorWithDraw(in *model.SupervisorWithDrawCreateRequest) *entity.SupervisorWithDraw {

	ety := &entity.SupervisorWithDraw{}

	ety.SupervisorId = in.SupervisorId

	ety.SupervisorName = in.SupervisorName

	ety.ItemList = in.ItemList

	ety.OrderCount = in.OrderCount

	ety.TotalAmount = in.TotalAmount

	ety.DeductAmount = in.DeductAmount

	ety.Desc = in.Desc

	ety.Status = in.Status

	ety.OperatorId = in.OperatorId

	ety.Operator = in.Operator

	ety.CreatedAt = time.Now().Unix()

	ety.UpdatedAt = in.UpdatedAt

	return ety
}
